package com.zlk.algorithm.huawei.nowcoder.graph.dfs;


import java.io.*;
import java.util.*;

/**
 * @program: algorithm
 * @ClassName HJ43
 * @description:
 * @author: slfang
 * @create: 2024-12-26 16:16
 * @Version 1.0
 **/
public class HJ43 {


    //dfs
    public static int N,M;
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(reader);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        while (in.nextToken()!=-1) {
            N = (int) in.nval;
            in.nextToken();
            M = (int) in.nval;
            int[][] martx = new int[N][M];
            for (int i = 0; i < N; i++) {
                for (int j = 0; j <M ; j++) {
                    in.nextToken();
                    martx[i][j] = (int) in.nval;
                }
            }
            // dfs
            //dfs(martx);
            //bfs
            bfs(martx);
        }
        out.flush();
        out.close();
        reader.close();
    }

    private static void bfs(int[][] martx) {
        Queue<String> queue = new LinkedList<>();
        HashSet<String> set = new HashSet<>();
        List<String> list = new ArrayList<>();
        String start = "(0,0)";
        list.add(start);
        queue.add(start);
        set.add(start);
        String end = "("+(N-1)+","+(M-1)+")";
        while (!queue.isEmpty()){
            String poll = queue.poll();
            if(end.equals(poll)){
                break;
            }
            String info = poll.replace("(","").replace(")","");
            String[] infoArr = info.split(",");
            int n = Integer.valueOf(infoArr[0]);
            int m = Integer.valueOf(infoArr[1]);
            if(n-1>=0){
                if(martx[n-1][m]==0){
                    String cur = getIndex(n-1,m);
                    if(!set.contains(cur)){
                        list.add(cur);
                        set.add(cur);
                        queue.add(cur);
                    }
                }
            }
            if(n+1<N){
                if(martx[n+1][m]==0){
                    String cur = getIndex(n+1,m);
                    if(!set.contains(cur)){
                        list.add(cur);
                        set.add(cur);
                        queue.add(cur);
                    }
                }
            }
            if(m+1<M){
                if(martx[n][m+1]==0){
                    String cur = getIndex(n,m+1);
                    if(!set.contains(cur)){
                        list.add(cur);
                        set.add(cur);
                        queue.add(cur);
                    }
                }
            }
            if(m-1>=0){
                if(martx[n][m-1]==0){
                    String cur = getIndex(n,m-1);
                    if(!set.contains(cur)){
                        list.add(cur);
                        set.add(cur);
                        queue.add(cur);
                    }
                }
            }
        }
        for (String s : list) {
            System.out.println(s);
        }

    }

    private static void dfs(int[][] martx) {
        Stack<String> stack = new Stack<>();
        String start = "(0,0)";
        stack.add(start);
        HashSet<String> set  = new HashSet<>();
        set.add(start);
        String end = "("+(N-1)+","+(M-1)+")";
        while (!stack.isEmpty()){
            String pop = stack.pop();
            if(end.equals(pop)){
                stack.add(pop);
                break;
            }
            String info = pop.replace("(","").replace(")","");
            String[] infoArr = info.split(",");
            int n = Integer.valueOf(infoArr[0]);
            int m = Integer.valueOf(infoArr[1]);
            if(n-1>=0){
                if(martx[n-1][m]==0){
                    String cur = getIndex(n-1,m);
                    if(!set.contains(cur)){
                        set.add(cur);
                        stack.add(pop);
                        stack.add(cur);
                        continue;
                    }
                }
            }
            if(n+1<N){
                if(martx[n+1][m]==0){
                    String cur = getIndex(n+1,m);
                    if(!set.contains(cur)){
                        set.add(cur);
                        stack.add(pop);
                        stack.add(cur);
                        continue;
                    }
                }
            }
            if(m+1<M){
                if(martx[n][m+1]==0){
                    String cur = getIndex(n,m+1);
                    if(!set.contains(cur)){
                        set.add(cur);
                        stack.add(pop);
                        stack.add(cur);
                        continue;
                    }
                }
            }
            if(m-1>=0){
                if(martx[n][m-1]==0){
                    String cur = getIndex(n,m-1);
                    if(!set.contains(cur)){
                        set.add(cur);
                        stack.add(pop);
                        stack.add(cur);
                        continue;
                    }
                }
            }
        }
        List<String> ansList=new ArrayList<>();
        while (!stack.isEmpty()){
            ansList.add(stack.pop());
        }
        for (int i = ansList.size()-1; i>=0; i--) {
            System.out.println(ansList.get(i));
        }
    }

    private static String getIndex(int n, int m) {
        return "("+n+","+m+")";
    }

    /// bfs


}
